package cn.com.dhcc.turbo.service.monitor.gather;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.util.CollectionUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.turbo.entity.monitor.QuotaValueOnTime;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
import cn.com.dhcc.turbo.entity.registry.DomainInfo;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.exch.filedata.RecvFileDataService;
import cn.com.dhcc.turbo.service.exch.filedata.SendFileDataService;
import cn.com.dhcc.turbo.service.monitor.MonitorService;
import cn.com.dhcc.turbo.service.monitor.QUOTA;
import cn.com.dhcc.turbo.service.registry.AdapterInfoService;
import cn.com.dhcc.turbo.service.registry.DomainInfoService;
import cn.com.dhcc.turbo.service.registry.NodeInfoService;

/**
 * @author zss
 * @2014/6/19 @12:47
 */
@Service
public class UnStructGatherService {
    @SuppressWarnings("deprecation")
	Logger logger = Log.getExistLogger(Log.LOGTYPE.MONITOR);
    @Autowired
    private SendFileDataService sendFileDataService;
    @Autowired
    private RecvFileDataService recvFileDataService;
    @Autowired
    NodeInfoService nodeInfoService;
   
    @Autowired
    MonitorService monitorService;
    @Autowired
    QuotaGatherUtil quotaGatherUtil;
    @Autowired
	DomainInfoService domainInfoService;
    @Autowired
	AdapterInfoService adapterService;
    /**
     * 每小时发送量，针对每个系统
     */

    public void unstructuredSendFlowHourForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_SEND_FLOW_HOUR_FOR_ADAPTER;
        String num = "";
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
        try {
        	List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(now);
    		calendar.set(Calendar.MINUTE, 0);
    		calendar.set(Calendar.SECOND, 0);
    		calendar.add(Calendar.HOUR, -1);
    		Date begintime = calendar.getTime();
    		calendar.add(Calendar.HOUR, 1);
    		Date endtime = calendar.getTime();

    		for (AdapterInfo adapter : adapterList) {
				String code = adapter.getAdapterCode();
                num = sendFileDataService.getSendCount(code, begintime, endtime);
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(begintime);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
            	onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(num);
                monitorService.insertQuotaOnTime(onTime);
            }
        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        }
        logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);

    }

    /**
     * 每小时接收量，针对每个系统
     */

    public void unstructuredRecvFlowHourForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_RECV_FLOW_HOUR_FOR_ADAPTER;
        String num = "";
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
       try {
    	   List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(now);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date endtime = calendar.getTime();//结束时间
            calendar.add(Calendar.HOUR, -1);
            Date begintime = calendar.getTime();//开始时间。提前一个小时
           for (AdapterInfo adapter : adapterList) {
                String code = adapter.getAdapterCode();
                num = recvFileDataService.getRecvCount(code, begintime, endtime);
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(begintime);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
               onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(num);
                monitorService.insertQuotaOnTime(onTime);
                logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
            }
        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        }


    }

    /**
     * 等待发送量，针对每个系统
     */
    public void unstructuredSendWaitNumForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_SEND_WAIT_NUM_FOR_ADAPTER;
        String num = "";
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
        try {
            List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
           for (AdapterInfo adapter : adapterList) {
                String code = adapter.getAdapterCode();
                num = sendFileDataService.getSendWait(code);
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(now);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
               onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(num);
                monitorService.insertQuotaOnTime(onTime);
                logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);

            }

        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        }

    }

    /**
     * 发送中量，针对每个系统
     */
    public void unstructuredSendSendingNumForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_SEND_SENDING_NUM_FOR_ADAPTER;
        String num = "";
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
        try {
            List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
           for (AdapterInfo adapter : adapterList) {
                String code = adapter.getAdapterCode();
                num = sendFileDataService.getSendSending(code);
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(now);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
               onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(num);
                monitorService.insertQuotaOnTime(onTime);
                logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
            }


        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        }

    }

    /**
     * 发送成功量，针对每个系统
     */

    public void unstructuredSendSuccessNumForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_SEND_SUCCESS_NUM_FOR_ADAPTER;
        String num = "";
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
        try {
            List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
           for (AdapterInfo adapter : adapterList) {
                String code = adapter.getAdapterCode();
                num = sendFileDataService.getSendSuccess(code);
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(now);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
               onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(num);
                monitorService.insertQuotaOnTime(onTime);
                logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
            }


        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        }
    }

    /**
     * 发送失败量，针对每个系统
     */

    public void unstructuredSendFailNumForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_SEND_FAIL_NUM_FOR_ADAPTER;
        String num = "";
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
        try {
            List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
           for (AdapterInfo adapter : adapterList) {
                String code = adapter.getAdapterCode();
                num = sendFileDataService.getSendFail(code);
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(now);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
               onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(num);
                monitorService.insertQuotaOnTime(onTime);
                logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
            }


        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        }

    }
    /**
     * 发送成功率，针对每个系统
     */

    public void unstructuredSendSuccessRateForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_SEND_SUCCESS_RATE_FOR_ADAPTER;
        int wait = 0;
        int sending = 0;
        int success = 0;
        int fail = 0;
        int sum = 0;
        String rate = "" ;
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
       try {
            List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
           for (AdapterInfo adapter : adapterList) {
                String code = adapter.getAdapterCode();
                wait = Integer.parseInt(sendFileDataService.getSendWait(code) != null ? sendFileDataService.getSendWait(code):"0");
                sending = Integer.parseInt(sendFileDataService.getSendSending(code)!= null ? sendFileDataService.getSendSending(code):"0");
                success = Integer.parseInt (sendFileDataService.getSendSuccess(code)!= null ? sendFileDataService.getSendSuccess(code):"0");
                fail =  Integer.parseInt (sendFileDataService.getSendFail(code)!= null ? sendFileDataService.getSendFail(code):"0");
                sum = wait+sending+success+fail;
				if(sum >0){
					rate = Math.round(Math.floor((float)success/sum*100))+"";
				}else{
					rate = "0";
				}
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(now);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
               onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(rate);
                monitorService.insertQuotaOnTime(onTime);
                logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
            }


        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        }
    }

    /**
     * 接收成功量，针对每个系统
     */
    public void unstructuredRecvSuccessNumForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_RECV_SUCCESS_NUM_FOR_ADAPTER;
        String num = "";
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
       try {
            List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
           for (AdapterInfo adapter : adapterList) {
                String code = adapter.getAdapterCode();
                num = recvFileDataService.getRecvSuccess(code);
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(now);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
               onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(num);
                monitorService.insertQuotaOnTime(onTime);
                logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
            }


        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        }

    }

    /**
     * 接收失败量，针对每个系统
     */
    public void unstructuredRecvFailNumForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_RECV_FAIL_NUM_FOR_ADAPTER;
        String num = "";
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
       try {
            List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
           for (AdapterInfo adapter : adapterList) {
                String code = adapter.getAdapterCode();
                num = recvFileDataService.getRecvFail(code);
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(now);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
               onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(num);
                monitorService.insertQuotaOnTime(onTime);
                logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
            }


        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
       }

    }

    /**
     * 接收成功率，针对每个系统
     */
    public void unstructuredRecvSuccessRateForAdapter() {
        QUOTA quota = QUOTA.UNSTRUCTURED_RECV_SUCCESS_RATE_FOR_ADAPTER;
        int success = 0;
        int fail = 0;
        int sum = 0;
        String rate = "" ;
        logger.info("开始收集指标[" + quota.getDesc() + "]...");
       try {
            List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
            Date now = new Date();
           for (AdapterInfo adapter : adapterList) {
                String code = adapter.getAdapterCode();
                success = Integer.parseInt (recvFileDataService.getRecvSuccess(code)!= null ? recvFileDataService.getRecvSuccess(code):"0");
                fail =  Integer.parseInt (recvFileDataService.getRecvFail(code)!= null ? recvFileDataService.getRecvFail(code):"0");
                sum = success+fail;
				if(sum >0){
					rate = Math.round(Math.floor((float)success/sum*100))+"";
				}else{
					rate = "0";
				}
                QuotaValueOnTime onTime = new QuotaValueOnTime();
                onTime.setQuotaCode(quota.getCode());
                onTime.setMtime(now);
                onTime.setId(UUIDGenerator.getUUID());
                onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
               onTime.setOwnerId(adapter.getId());
                onTime.setCurrentValue1(rate);
                monitorService.insertQuotaOnTime(onTime);
                logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
            }


        } catch (ServiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        } catch (NiceException e) {
            logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
        }

    }
    /**
	 * 节点发送成功率
	 */
	public void unstructuredSendSuccessRateForNode() {
		QUOTA quota = QUOTA.UNSTRUCTURED_SEND_SUCCESS_RATE_FOR_NODE;
		int wait = 0;
		int sending = 0;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			Date now = new Date();
			NodeInfo localNode = nodeInfoService.getLocalNode();
			if(localNode==null){
				logger.error("自动根据系统指标值计算节点指标指标失败:节点未初始化");
				return;
			}
			//待发送
			List<QuotaValueOnTime> waitValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.UNSTRUCTURED_SEND_WAIT_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(waitValueOnTimes)){
				wait =  Integer.parseInt(waitValueOnTimes.get(0).getCurrentValue1());
			}
			//发送中
			List<QuotaValueOnTime> sendingValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.UNSTRUCTURED_SEND_SENDING_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(sendingValueOnTimes)){
				sending =  Integer.parseInt(sendingValueOnTimes.get(0).getCurrentValue1());
			}
			//发送成功
			List<QuotaValueOnTime> successValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.UNSTRUCTURED_SEND_SUCCESS_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(successValueOnTimes)){
				success =  Integer.parseInt(successValueOnTimes.get(0).getCurrentValue1());
			}
			//发送失败
			List<QuotaValueOnTime> failValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.UNSTRUCTURED_SEND_FAIL_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(failValueOnTimes)){
				fail =  Integer.parseInt(failValueOnTimes.get(0).getCurrentValue1());
			}
			sum = wait+sending+success+fail;
			if(sum >0){
				rate = Math.round(Math.floor((float)success/sum*100))+"";
			}else{
				rate = "0";
			}
			QuotaValueOnTime onTime = new QuotaValueOnTime();
			onTime.setQuotaCode(quota.getCode());
			onTime.setMtime(now);
			onTime.setId(UUIDGenerator.getUUID());
			onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
			onTime.setOwnerId(localNode.getId());
			onTime.setCurrentValue1(rate);
			monitorService.insertQuotaOnTime(onTime);
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
	/**
	 * 节点接收成功率
	 */
	public void unstructuredRecvSuccessRateForNode() {
		QUOTA quota = QUOTA.UNSTRUCTURED_RECV_SUCCESS_RATE_FOR_NODE;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			Date now = new Date();
			NodeInfo localNode = nodeInfoService.getLocalNode();
			if(localNode==null){
				logger.error("自动根据系统指标值计算节点指标指标失败:节点未初始化");
				return;
			}
			//接收成功
			List<QuotaValueOnTime> successValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.UNSTRUCTURED_RECV_SUCCESS_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(successValueOnTimes)){
				success =  Integer.parseInt(successValueOnTimes.get(0).getCurrentValue1());
			}
			//接收失败
			List<QuotaValueOnTime> failValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.UNSTRUCTURED_RECV_FAIL_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(failValueOnTimes)){
				fail =  Integer.parseInt(failValueOnTimes.get(0).getCurrentValue1());
			}
			sum = success+fail;
			if(sum >0){
				rate = Math.round(Math.floor((float)success/sum*100))+"";
			}else{
				rate = "0";
			}
			QuotaValueOnTime onTime = new QuotaValueOnTime();
			onTime.setQuotaCode(quota.getCode());
			onTime.setMtime(now);
			onTime.setId(UUIDGenerator.getUUID());
			onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
			onTime.setOwnerId(localNode.getId());
			onTime.setCurrentValue1(rate);
			monitorService.insertQuotaOnTime(onTime);
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
	
	/**
	 * 域发送成功率
	 */
	public void unstructuredSendSuccessRateForDomain() {
		QUOTA quota = QUOTA.UNSTRUCTURED_SEND_SUCCESS_RATE_FOR_DOMAIN;
		int wait = 0;
		int sending = 0;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			Date now = new Date();
			List<DomainInfo> domainInfos = domainInfoService.list(null);
			if(CollectionUtil.hasElement(domainInfos)){
				return;
			}
			for (Iterator<DomainInfo> iterator = domainInfos.iterator(); iterator.hasNext();) {
				DomainInfo domainInfo =  iterator.next();
				String domainId = domainInfo.getId();
				//待发送
				List<QuotaValueOnTime> waitValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.UNSTRUCTURED_SEND_WAIT_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(waitValueOnTimes)){
					wait =  Integer.parseInt(waitValueOnTimes.get(0).getCurrentValue1());
				}
				//发送中
				List<QuotaValueOnTime> sendingValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.UNSTRUCTURED_SEND_SENDING_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(sendingValueOnTimes)){
					sending =  Integer.parseInt(sendingValueOnTimes.get(0).getCurrentValue1());
				}
				//发送成功
				List<QuotaValueOnTime> successValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.UNSTRUCTURED_SEND_SUCCESS_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(successValueOnTimes)){
					success =  Integer.parseInt(successValueOnTimes.get(0).getCurrentValue1());
				}
				//发送失败
				List<QuotaValueOnTime> failValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.UNSTRUCTURED_SEND_FAIL_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(failValueOnTimes)){
					fail =  Integer.parseInt(failValueOnTimes.get(0).getCurrentValue1());
				}
				sum = wait+sending+success+fail;
				if(sum >0){
					rate = Math.round(Math.floor((float)success/sum*100))+"";
				}else{
					rate = "0";
				}
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(domainId);
				onTime.setCurrentValue1(rate);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
	/**
	 * 域接收成功率
	 */
	public void unstructuredRecvSuccessRateForDomain() {
		QUOTA quota = QUOTA.UNSTRUCTURED_RECV_SUCCESS_RATE_FOR_DOMAIN;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			Date now = new Date();
			List<DomainInfo> domainInfos = domainInfoService.list(null);
			if(CollectionUtil.hasElement(domainInfos)){
				return;
			}
			for (Iterator<DomainInfo> iterator = domainInfos.iterator(); iterator.hasNext();) {
				DomainInfo domainInfo =  iterator.next();
				String domainId = domainInfo.getId();
				//接收成功
				List<QuotaValueOnTime> successValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.UNSTRUCTURED_RECV_SUCCESS_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(successValueOnTimes)){
					success =  Integer.parseInt(successValueOnTimes.get(0).getCurrentValue1());
				}
				//接收失败
				List<QuotaValueOnTime> failValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.UNSTRUCTURED_RECV_FAIL_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(failValueOnTimes)){
					fail =  Integer.parseInt(failValueOnTimes.get(0).getCurrentValue1());
				}
				sum = success+fail;
				if(sum >0){
					rate = Math.round(Math.floor((float)success/sum*100))+"";
				}else{
					rate = "0";
				}
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(domainId);
				onTime.setCurrentValue1(rate);
				monitorService.insertQuotaOnTime(onTime);
			}
			
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
    /**
     * 在收集完系统监控值后执行
     */
    public void autoGatherNodeAndDomainOntimeValue() {
        QUOTA sysQuotas[] =
                {
                        QUOTA.UNSTRUCTURED_SEND_WAIT_NUM_FOR_ADAPTER,
                        QUOTA.UNSTRUCTURED_SEND_SENDING_NUM_FOR_ADAPTER,
                        QUOTA.UNSTRUCTURED_SEND_SUCCESS_NUM_FOR_ADAPTER,
                        QUOTA.UNSTRUCTURED_SEND_FAIL_NUM_FOR_ADAPTER,
                        QUOTA.UNSTRUCTURED_RECV_SUCCESS_NUM_FOR_ADAPTER,
                        QUOTA.UNSTRUCTURED_RECV_FAIL_NUM_FOR_ADAPTER
                };
        try {
            quotaGatherUtil.gatherValueForNodeByAdapterLastValue(sysQuotas);
            unstructuredSendSuccessRateForNode();
            unstructuredRecvSuccessRateForNode();
            quotaGatherUtil.gatherValueForDomainByAdapterLastValue(sysQuotas);
            unstructuredSendSuccessRateForDomain();
            unstructuredRecvSuccessRateForDomain();
        } catch (ServiceException e) {
            logger.error("自动计算指标值失败!", e);
        }

    }

    public void autoGatherNodeAndDomainOntimeForFlowValue() {
        QUOTA sysQuotas[] =
                {
                        QUOTA.UNSTRUCTURED_SEND_FLOW_HOUR_FOR_ADAPTER,
                        QUOTA.UNSTRUCTURED_RECV_FLOW_HOUR_FOR_ADAPTER,

                };
        try {
            quotaGatherUtil.gatherValueForNodeByAdapterLastValue(sysQuotas);
            quotaGatherUtil.gatherValueForDomainByAdapterLastValue(sysQuotas);
        } catch (ServiceException e) {
            logger.error("自动计算指标值失败!", e);
        }

    }

}
